package com.jjyu.controller;

import com.jjyu.entity.Favorite;
import com.jjyu.entity.Result;
import com.jjyu.service.FavoriteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/favorites")
public class FavoriteController {
    @Autowired
    private FavoriteService favoriteService;

    // 添加餐厅收藏
    @PostMapping
    public Result addFavorite(@RequestBody Favorite favorite) {
        // 检查用户是否已收藏该餐厅
        if (favoriteService.is_existsFavorite(favorite.getUserId(), favorite.getRestaurantId())) {
            return Result.error("已收藏该餐厅");
        }
        favoriteService.save(favorite);
        return Result.success("餐厅收藏成功");
    }

    // 获取用户的所有餐厅收藏
    @GetMapping("/user/{userId}")
    public Result<List<Favorite>> getFavoritesByUserId(@PathVariable("userId") Integer userId) {
        List<Favorite> favorites = favoriteService.getFavoritesByUserId(userId);
        return Result.success(favorites);
    }

    // 删除餐厅收藏
    @DeleteMapping("/{id}")
    public Result deleteFavorite(@PathVariable("id") Integer id) {
        favoriteService.removeById(id);
        return Result.success("餐厅收藏已删除");
    }
}
